﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for AdminTimeSheetService
    /// </summary>
    public class AdminTimeSheetService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            var model = new TIMESHEET_EMPLOYEE();
            var lst = new List<TIMESHEET_EMPLOYEE>();

            var site = request["site"];
            var _cmd = new Project_Service();
            var listProjec = _cmd.GetProjectNameAllById(Convert.ToInt16(site));

            foreach (var p in listProjec)
            {
                if (p.OTS_ASSIGN_PROJECT.Count > 0)
                {
                    model = new TIMESHEET_EMPLOYEE();
                    model.ID = p.OTS_ASSIGN_PROJECT.FirstOrDefault().EMPLOYEE_ID;
                    model.NAME = getEmpNamebyID(model.ID);
                    model.POSITION = getPositionbyID(model.ID);
                    model.PROJECT = p.NAME;
                    model.SITE = p.OTS_SITE.NAME;
                    model.WORK_STATUS = getProjectStatus(p.STATUS);
                    model.TYPE = getEmpTypebyID(model.ID);
                    lst.Add(model);
                }

            }
            lst = lst.OrderBy(o => o.ID).ToList();

            int total = lst.Count == 0 ? 1 : (int)Math.Ceiling((double)lst.Count / 10);

            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = lst.Count,
                rows = lst.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }

        private List<OTS_EMPLOYEE> _empList = new List<OTS_EMPLOYEE>();
        private void initialListEmp()
        {
            var cmd = new Employee_Service();
            _empList = cmd.GetAll();

        }
        private string getEmpNamebyID(string emp_id)
        {
            if (_empList.Count ==0)
            {
                initialListEmp();
            }
            var emp_name = _empList.Where(x => x.ID == emp_id).FirstOrDefault();
            if (emp_name != null)
            {
                if (emp_name.NAME_EN == "")
                {
                    return emp_name.NAME_TH;
                }
                else
                {
                    return emp_name.NAME_EN;
                }
            }
            else
            {
                return "";
            }
        }

        private string getPositionbyID(string emp_id)
        {
            if (_empList.Count == 0)
            {
                initialListEmp();
            }
            var emp_name = _empList.Where(x => x.ID == emp_id).FirstOrDefault();
            if (emp_name != null)
            {
                return emp_name.POSITION;
            }
            else
            {
                return "";
            } 
        }

        private string getEmpTypebyID(string emp_id)
        {
            if (_empList.Count == 0)
            {
                initialListEmp();
            }
            var emp_name = _empList.Where(x => x.ID == emp_id).FirstOrDefault();
            if (emp_name != null)
            {
                return emp_name.TYPE;
            }
            else
            {
                return "";
            }

        }
        private string getProjectStatus(bool? status)
        {
            if (status == true)
            {
                return "Opening";

            }
            else
            {
                return "Close";
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}